package org.dromara.administration.controller;

import java.util.List;

import cn.hutool.core.lang.tree.Tree;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.administration.domain.bo.AdmSealSaveBo;
import org.dromara.system.domain.bo.SysDeptBo;
import org.dromara.system.service.ISysDeptService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.administration.domain.vo.AdmSealVo;
import org.dromara.administration.domain.bo.AdmSealBo;
import org.dromara.administration.service.IAdmSealService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.administration.domain.vo.AdmSealAuditVo;
import org.dromara.administration.domain.bo.AdmSealAuditBo;

/**
 * 公章管理
 *
 * @author LUSH
 * @date 2025-05-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/adm/seal")
public class AdmSealController extends BaseController {

    private final IAdmSealService qyxAdminSealService;

    private final ISysDeptService deptService;

    /**
     * 查询公章管理列表
     */
    @SaCheckPermission("administration:seal:list")
    @GetMapping("/list")
    public TableDataInfo<AdmSealVo> list(AdmSealBo bo, PageQuery pageQuery) {
        return qyxAdminSealService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出公章管理列表 待优化
     */
    @SaCheckPermission("administration:seal:export")
    @Log(title = "公章管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AdmSealBo bo, HttpServletResponse response) {
        List<AdmSealVo> list = qyxAdminSealService.queryList(bo);
        ExcelUtil.exportExcel(list, "公章管理", AdmSealVo.class, response);
    }

    /**
     * 获取公章管理详细信息（用于修改回显）
     *
     * @param id 主键
     */
    @SaCheckPermission("administration:seal:query")
    @GetMapping("/{id}")
    public R<AdmSealSaveBo> getInfo(@NotNull(message = "主键不能为空")
                                         @PathVariable Long id) {
        return R.ok(qyxAdminSealService.getEditInfoById(id));
    }

    /**
     * 新增公章管理
     */
    @SaCheckPermission("administration:seal:add")
    @Log(title = "公章管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AdmSealSaveBo bo) {
        return toAjax(qyxAdminSealService.insertByBo(bo));
    }

    /**
     * 修改公章管理
     */
    @SaCheckPermission("administration:seal:edit")
    @Log(title = "公章管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AdmSealSaveBo bo) {
        return toAjax(qyxAdminSealService.updateByBo(bo));
    }

    /**
     * 删除公章管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("administration:seal:remove")
    @Log(title = "公章管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(qyxAdminSealService.deleteWithValidByIds(List.of(ids)));
    }

    /**
     * 获取当前要添加的公章申请编号
     */
    @SaCheckPermission("administration:seal:number")
    @GetMapping("/number")
    public R<String> getNextSealNumber() {
        String number = qyxAdminSealService.generateNextSealNumber();
        return R.ok("请求成功",number);
    }

    /**
     * 获取公章申请审核详情（含用户名和部门名）
     */
    @SaCheckPermission("administration:seal:audit")
    @GetMapping("/audit/{id}")
    public R<AdmSealAuditVo> getAuditInfo(@PathVariable Long id, @RequestParam String auditType) {
        return R.ok(qyxAdminSealService.getAuditInfoById(id, auditType));
    }

    /**
     * 公章审核
     */
    @SaCheckPermission("administration:seal:audit")
    @Log(title = "公章审核", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/audit")
    public R<Void> audit(@Validated @RequestBody AdmSealAuditBo bo) {
        return toAjax(qyxAdminSealService.auditSeal(bo));
    }

    /**
     * 获取部门树列表
     */
    @SaCheckPermission("administration:user:list")
    @GetMapping("/deptTree")
    public R<List<Tree<Long>>> deptTree(SysDeptBo dept) {
        return R.ok(deptService.selectDeptTreeList(dept));
    }



}
